#       Weather_test.py
#       
#       Copyright 2008 Joshua Scotton <josh@joshuascotton.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2008, Joshua Scotton"
__author__ = "Joshua Scotton <josh@joshuascotton.com>"

import unittest
import os
import sys

from xml.dom import minidom 

sys.path += [os.path.abspath('..')]

from utils.weather import Weather, WeatherForecast

class WeatherTest(unittest.TestCase):

    def setUp(self):
        """Sets up everything for the test"""
        self.weather = Weather("UKXX0040","f")
        self.forecast = WeatherForecast("Sat", "01/01/2008", 18, 20, "Sunny", 32, "C")
    
    def testWeatherInit001(self):
        #__init__(self, locID="", type="f"):
        self.assertEqual(self.weather.get_location_id(),"UKXX0040")
    def testWeatherInit002(self):
        #__init__(self, locID="", type="f"):
        self.assertEqual(self.weather.get_type(),"f")
    def testWeatherSearch(self):
        #search(self, search_string):
        self.assertEqual(self.weather.search("derby"),[("UKXX0040","Europe > United Kingdom > Derby")])
        self.assertEqual(self.weather.search("washington dc"),[("USDC0001","North America > United States > Washington, D.C. > Washington")])
        self.assertEqual(self.weather.search("helsinki"),[("FIXX0002","Europe > Finland > Helsinki")])
    def testWeatherGetLocationFromID(self):
        #get_location_from_id(self, locID, url="http://weather.yahoo.com/forecast/"):
        self.assertEqual(self.weather.get_location_from_id("UKXX0040"),"Europe > United Kingdom > Derby")
        self.assertEqual(self.weather.get_location_from_id("USDC0001"),"North America > United States > Washington, D.C. > Washington")
        self.assertEqual(self.weather.get_location_from_id("FIXX0002"),"Europe > Finland > Helsinki")
    def testWeatherSetLocationID(self):
        #set_location_id(self, locID):
        self.weather.set_location_id("FIXX0002")
        self.assertEqual(self.weather.get_location_id(),"FIXX0002")
    def testWeatherGetLocationID(self):
        #get_location_id(self):
        self.assertEqual(self.weather.get_location_id(),"UKXX0040")
    def testWeatherSetTypeRecognised(self):
        #set_type(self, type):
        self.weather.set_type("c")
        self.assertEqual(self.weather.get_type(),"c")
        self.weather.set_type("f")
        self.assertEqual(self.weather.get_type(),"f")
        self.weather.set_type("C")
        self.assertEqual(self.weather.get_type(),"c")
        self.weather.set_type("F")
        self.assertEqual(self.weather.get_type(),"f")
    def testWeatherSetTypeUnrecognised(self):
        #set_type(self, type):
        self.weather.set_type("a")
        self.assertEqual(self.weather.get_type(),"c")
    def testWeatherGetType(self):
        #get_type(self):
        self.assertEqual(self.weather.get_type(),"f")

    def testWeatherClearCurrentDict(self):
        #clear_current_dict(self):
        self.weather.clear_current_dict()
        self.assertEqual({},self.weather.get_current_dict())
    def testWeatherSetCurrentNew(self):
        #set_current(self,key,value):
        self.weather.set_current("location.city","testDATA")
        self.assertEqual("testDATA", self.weather.get_current("location.city"))
    def testWeatherSetCurrentOverwriting(self):
        #set_current(self,key,value):
        self.weather.clear_current_dict()
        self.weather.set_current("test","testDATA")
        self.assertEqual("testDATA", self.weather.get_current("test"))
    def testWeatherClearForecasts(self):
        #clear_forecasts(self):
        self.weather.clear_forecasts()
        self.assertEqual([], self.weather.get_forecasts())
    def testWeatherAddForecast(self):
        #add_forecast(self, forecast):
        self.weather.clear_forecasts()
        self.weather.add_forecast(self.forecast)
        self.assertEqual(1, len(self.weather.get_forecasts()))
        self.assertEqual(self.forecast, self.weather.get_forecasts()[0])
    def testWeatherRefresh(self):
        #refresh(self,xml=""):raise Exception
        xmldoc = minidom.parse(os.path.abspath('./data/Weather/forecastrss.xml'))
        self.weather.refresh(xmldoc)
        self.assertEqual(self.weather.get_current("location.city"),"Derby")
        self.assertEqual(self.weather.get_current("location.region"),"")
        self.assertEqual(self.weather.get_current("location.country"),"UK")
        self.assertEqual(self.weather.get_current("units.temperature"),"F")
        self.assertEqual(self.weather.get_current("units.distance"),"mi")
        self.assertEqual(self.weather.get_current("units.pressure"),"in")
        self.assertEqual(self.weather.get_current("units.speed"),"mph")
        self.assertEqual(self.weather.get_current("wind.chill"),"33")
        self.assertEqual(self.weather.get_current("wind.direction"),"200")
        self.assertEqual(self.weather.get_current("wind.speed"),"14")
        self.assertEqual(self.weather.get_current("atmosphere.humidity"),"76")
        self.assertEqual(self.weather.get_current("atmosphere.visibility"),"6.21")
        self.assertEqual(self.weather.get_current("atmosphere.pressure"),"29.26")
        self.assertEqual(self.weather.get_current("atmosphere.rising"),"0")
        self.assertEqual(self.weather.get_current("astronomy.sunrise"),"6:34 am")
        self.assertEqual(self.weather.get_current("astronomy.sunset"),"6:00 pm")
        self.assertEqual(self.weather.get_forecasts()[0].day,"Sun")
        self.assertEqual(self.weather.get_forecasts()[0].date,"9 Mar 2008")
        self.assertEqual(self.weather.get_forecasts()[0].low,37)
        self.assertEqual(self.weather.get_forecasts()[0].high,46)
        self.assertEqual(self.weather.get_forecasts()[0].text,"Rain/Wind Late")
        self.assertEqual(self.weather.get_forecasts()[0].code,12)
        self.assertEqual(self.weather.get_forecasts()[0].type,"F")
    def testWeatherGetCurrent(self):
        #get_current(self,key):
        self.weather.set_current("testing","testDATA")
        self.assertEqual("testDATA", self.weather.get_current("testing"))
        
    def testWeatherGetRSSURL(self):
        #get_rss_url(self):
        self.assertEqual(self.weather.get_rss_url(),"http://xml.weather.yahoo.com/forecastrss?p=UKXX0040&u=f")
        
    def testWeatherLenGetCurrentDict(self):
        #get_current_dict(self):
        xmldoc = minidom.parse(os.path.abspath('./data/Weather/forecastrss.xml'))
        self.weather.refresh(xmldoc)
        self.assertEqual(len(self.weather.get_current_dict()),20)
    def testWeatherLenGetForecasts(self):
        #get_forecasts(self):
        xmldoc = minidom.parse(os.path.abspath('./data/Weather/forecastrss.xml'))
        self.weather.refresh(xmldoc)
        self.assertEqual(len(self.weather.get_forecasts()),5)
